#include "axes.h"

#include "qtcpp/common.h"

Axes::Axes(Ogre::SceneManager* sceneMng, Ogre::SceneNode* parent)
{
	m_pSceneMng = sceneMng;
	m_pSceneNode = parent->createChildSceneNode("Axes" + EasyLib::QtCpp::getUuid().toStdString());

	m_nRadius = 0.05;
	m_nLength = 1;
	m_pXAxis = nullptr;
	m_pYAxis = nullptr;
	m_pZAxis = nullptr;

	init();
}

Axes::~Axes()
{
	m_pSceneMng->destroyEntity(m_pXAxis);
	m_pSceneMng->destroyEntity(m_pYAxis);
	m_pSceneMng->destroyEntity(m_pZAxis);

	m_pSceneMng->destroySceneNode(m_pSceneNode);
}


void Axes::update(PropGroup group)
{
	if (group.name != "Axes")
		return;

	if (group.data.size() != 2)
		return;

	m_nRadius = group.data[0].toFloat();
	m_nLength = group.data[1].toFloat();
	update();
}

void Axes::update()
{
	m_pXAxis->getParentSceneNode()->setScale(m_nRadius, m_nLength, m_nRadius);
	m_pYAxis->getParentSceneNode()->setScale(m_nRadius, m_nLength, m_nRadius);
	m_pZAxis->getParentSceneNode()->setScale(m_nRadius, m_nLength, m_nRadius);

	m_pXAxis->getParentSceneNode()->setPosition(Ogre::Vector3(m_nLength / 2.0f, 0.0f, 0.0f));
	m_pXAxis->getParentSceneNode()->setOrientation(Ogre::Quaternion(Ogre::Degree(-90), Ogre::Vector3::UNIT_Z));
	m_pYAxis->getParentSceneNode()->setPosition(Ogre::Vector3(0.0f, m_nLength / 2.0f, 0.0f));
	m_pZAxis->getParentSceneNode()->setPosition(Ogre::Vector3(0.0, 0.0f, m_nLength / 2.0f));
	m_pZAxis->getParentSceneNode()->setOrientation(Ogre::Quaternion(Ogre::Degree(90), Ogre::Vector3::UNIT_X));
}

PropGroup Axes::getPropGroup()
{
	PropGroup group;
	group.name = "Axes";
	group.uuid = QString::fromStdString(m_pSceneNode->getName());
	group.label = {"Radius","Length"};
	group.type = { "float","int"};
	group.data = { QString::number(m_nRadius),QString::number(m_nLength) };
	group.min = { "0.01","1" };
	group.max = { "","" };
	group.step = { "0.01","1" };
	return group;
}

Ogre::SceneNode* Axes::getSceneNode()
{
	return m_pSceneNode;
}

void Axes::init()
{
	m_pXAxis = m_pSceneMng->createEntity("cylinder.mesh");
	m_pYAxis = m_pSceneMng->createEntity("cylinder.mesh");
	m_pZAxis = m_pSceneMng->createEntity("cylinder.mesh");

	auto xAxisNode = m_pSceneNode->createChildSceneNode();
	auto yAxisNode = m_pSceneNode->createChildSceneNode();
	auto zAxisNode = m_pSceneNode->createChildSceneNode();
	xAxisNode->attachObject(m_pXAxis);
	yAxisNode->attachObject(m_pYAxis);
	zAxisNode->attachObject(m_pZAxis);

	m_pXAxis->setMaterialName("red");
	m_pYAxis->setMaterialName("green");
	m_pZAxis->setMaterialName("blue");

	update();
}
